package middleware

import (
	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"strings"
)

//不需要鉴权的uri
var ignoreUri map[string]interface{}

func init() {
	ignoreUri = make(map[string]interface{})
	ignoreUri["/back/user/login"] = ""
	ignoreUri["/back/user/sign"] = ""
}

// Auth 鉴权中间件
func Auth() gin.HandlerFunc {
	return func(c *gin.Context) {
		/*uri := c.Request.RequestURI
		if !isAuthUri(uri) {
			c.Next()
			return
		}*/
		session := sessions.Default(c)
		v := session.Get("hello")
		logrus.Info("session v:", v)
		if v == nil {
			session.Set("hello", "world")
			err := session.Save()
			if err != nil {
				panic(err)
			}
		}
		//token校验
		/*	token := c.Request.Header.Get("token")
			if token == "" {
				c.AbortWithStatusJSON(403, model.Fail("未登陆"))
				return
			}*/
		// 处理请求
		c.Next()

	}
}

//判断是否是请求需要鉴权的的uri
func isAuthUri(uri string) bool {
	if isIgnoreUri(uri) {
		return false
	}
	return strings.Contains(uri, "/back")
}

//忽略鉴权的uri
func isIgnoreUri(uri string) bool {
	return ignoreUri[uri] != nil
}
